package com.kaching.twitter

import com.kaching.api.client.model._
import twitter4j.{User => TUser, Twitter}

abstract sealed class KcResponse {
  def message : String
}
case class OrderResponse(val orderStat: OrderStatus, val user: KCUser) extends KcResponse {
  def message = orderStat match {
    case stat: OrderStatusError => "processing error : " + getErrorMessage(stat.getError)
    case stat: OrderStatusWarning => { "http://www.kaching.com/portfolio/" + user.kcId + "/holdings " + 
      "warning for " + orderMessage(stat.getOrder) + 
      " : " + getWarnMessage(stat.getWarning)
    }
    case stat: OrderStatusSuccess => {"http://www.kaching.com/portfolio/" + user.kcId + "/holdings " + 
      "posted " + orderMessage(stat.getOrder) 
    } 
    case _ => "error parsing response!"
  }
  
  private def getErrorMessage(error: OrderStatusError.TheError) = error match {
    case ns: OrderStatusError.NoShares => "there are no shares to act on"
    case e: OrderStatusError.CouldNotConfirmOrder => "could not confirm order"
    case e: OrderStatusError.UnknownSecurity => "Unknown Security"
    case e: OrderStatusError.RealTimeFeedTimeout => "Real-Time Feed Timeout"
    case e: OrderStatusError.PriceEqualOrLessThanCommission => "Price Equal or less than Commission"
    case e: OrderStatusError.NegativeCash => "Negative Cash"
    case e: OrderStatusError.LimitOrderOnly => "Limit Order Only"
    case nes: OrderStatusError.NotEnoughShares => "Not Enough Shares, can act on only " + nes.getAllowedShares + " shares"
    case nee: OrderStatusError.NotEnoughEquity => "Not Enough Equity, can act on only " + nee.getAllowedShares + " shares"
    case nec: OrderStatusError.NotEnoughCash => "Not Enough Cache, can act on only " + nec.getAllowedShares + " shares"
    case nev: OrderStatusError.OverMaxVolume => "Over Max Volume, can act on only " + nev.getAllowedShares + " shares"
    case nev: OrderStatusError.LimitedVolume => "Limited Volume, can act on only " + nev.getAllowedShares + " shares"
    case nev: OrderStatusError.LimitedOutstandingShares => "Limited Outstanding Shares, can act on only " + nev.getAllowedShares + " shares"
    case nev: OrderStatusError.LimitedShortInterest => "Limited Short Interest, can act on only " + nev.getAllowedShares + " shares"
    case nev: OrderStatusError.NoVolume => "No Volume, can act on only " + nev.getAllowedShares + " shares"
    case other => throw new IllegalArgumentException("got error " + other.getClass + " - " + other)
  }
  
  private def getWarnMessage(warning: OrderStatusWarning.Warning) = warning match {
    case e: OrderStatusWarning.UnknownSecurity => "Unknown Security"
    case e: OrderStatusWarning.LimitOrderOnly => "Limit Order Only"
    case nee: OrderStatusWarning.NotEnoughEquity => "Not Enough Equity, can act on only " + nee.getAllowedShares + " shares"
    case nec: OrderStatusWarning.NotEnoughCash => "Not Enough Cache, can act on only " + nec.getAllowedShares + " shares"
    case nec: OrderStatusWarning.NotTradedToday => "Not Traded Today"
    case nec: OrderStatusWarning.MarketClosed => "Market Closed"
    case other => throw new IllegalArgumentException("got warning " + other.getClass + " - " + other)
  }
  
  private def orderMessage(order: Order) = order match {
    case order: MarketOrder => "market " + order.getAction.name.toLowerCase + " order for " + order.getQuantity + 
      " $" + order.getSymbol  
    case order: LimitOrder => "limit " + order.getAction.name.toLowerCase + " order for " + order.getQuantity + 
      " $" + order.getSymbol + " at $" + order.getLimit.toString 
  }
}

case class LinkResquest(val kcUser: KCUser) extends KcResponse {
  def message = """Please DM KaChing id & key (https://www.kaching.com/remotekey) in the format: DM @kc_trade 1234 my654key"""
}

case class LinkSuccessResponse(val kcUser: KCUser) extends KcResponse {
  def message = """congratulations! you have successfully linked your KaChing profile with Twitter"""  
}

case class ErrorResponse(val str: String) extends KcResponse {
  def message = """could not understand your message: """ + str 
}

